<?php

class MallTemplateController extends Controller
{
    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = MallTemplate::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = MallTemplate::model()->findAll($searchsql);


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");


            //添加
            $obj = new MallTemplate();
            Util::initObjData($obj);


            $obj->name = $name;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");


            //验证存在
            $obj = MallTemplate::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                //修改信息

                $obj->name = $name;

                //保存
                Util::saveObjData($obj);



                //返回数据
                Util::responseMsg(MsgConst::RETURN_SUCCESS);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = MallTemplate::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
     * 获取模板对应的规格信息
     */
    public function actionGetspecsInfo()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数选择
            $template_id = Util::getParamNotEmpty("template_id");


            //初始化
            $searchsql = Util::initSearchSql();
            $searchsql = $searchsql . " order by id desc";

            $returnlist = MallSpecs::model()->findAll($searchsql);

            $count = count($returnlist);

            for($i=0;$i<count($returnlist);$i++)
            {
                $returnlist[$i]->is_select = MallTemSpecsService::checkSpecsAndTem($returnlist[$i]->id,$template_id);
            }


            //Util::responseObj($returnlist);

            Util::responseList(count($returnlist),$returnlist);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }

    //设置模板对应的规格信息

    public function actionSetspecsInfo()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数选择
            $template_id = Util::getParamNotEmpty("template_id");



            $select_list = Util::getParam("select_list","");
            $unselect_list = Util::getParam("unselect_list","");

            if($select_list == "" && $unselect_list == "")
                throw new Exception("select_list 和 select_list 不能同时为空");

            //设置
            MallTemSpecsService::setTemSpecsRelation($template_id,$select_list,$unselect_list);



            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }

}